package com.zy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.common.result.Result;
import com.zy.model.system.SysMenu;
import com.zy.model.system.SysRoleMenu;
import com.zy.model.vo.AssginMenuVo;
import com.zy.model.vo.RouterVo;
import com.zy.system.exception.ZyException;
import com.zy.system.mapper.SysMenuMapper;
import com.zy.system.mapper.SysRoleMenuMapper;
import com.zy.system.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.system.utils.MenuHelper;
import com.zy.system.utils.RouterHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author Gongxl
 * @since 2023-06-09
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findNodes() {
        List<SysMenu> sysMenusList = baseMapper.selectList(null);


        //构建树形数据
        List<SysMenu> result = MenuHelper.buildTree(sysMenusList);
        return result;

    }

    @Override
    public void removeMenuById(Long id) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>( );
        wrapper.eq("parent_id" ,id);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0){//有子菜单
            throw new ZyException(201,"请先删除子菜单");
        }
        //调用删除
        baseMapper.deleteById(id);
    }
    //获取角色已分配的菜单
    @Override
    public List<SysMenu> findSysMenuByRoleId(String roleId) {
        //查询所有菜单 status=1
        List<SysMenu> menuList = baseMapper.selectList(new QueryWrapper<SysMenu>().eq("status", 1));
        //根据角色id获取角色权限-角色已分配的菜单
        List<SysRoleMenu> roleMenus = sysRoleMenuMapper.selectList(new QueryWrapper<SysRoleMenu>().eq("role_id",roleId));
        //获取该角色已分配的所有权限id
        List<String> roleMenuIds = new ArrayList<>();
        for (SysRoleMenu roleMenu : roleMenus) {
            roleMenuIds.add(roleMenu.getMenuId());
        }
        //遍历所有权限列表
        for (SysMenu sysMenu : menuList) {
            if(roleMenuIds.contains(sysMenu.getId())){
                //设置该权限已被分配
                sysMenu.setSelect(true);
            }else {
                sysMenu.setSelect(false);
            }
        }
        //将权限列表转换为权限树
        List<SysMenu> sysMenus = MenuHelper.buildTree(menuList);
        return sysMenus;
    }
    //给角色分配菜单权限
    @Override
    public void doAssign(AssginMenuVo assginMenuVo) {
        //删除已分配的权限
        sysRoleMenuMapper.delete(new QueryWrapper<SysRoleMenu>().eq("role_id",assginMenuVo.getRoleId()));
        //遍历所有已选择的权限id
        for (String  menuId : assginMenuVo.getMenuIdList()) {
            if(menuId != null){
                //创建SysRoleMenu对象
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuId(menuId);
                sysRoleMenu.setRoleId(assginMenuVo.getRoleId());
                //添加新权限
                sysRoleMenuMapper.insert(sysRoleMenu);
            }
        }
    }
    //查询菜单
    @Override
    public List<RouterVo> getUserMenuList(String userId) {
        //菜单集合
        List<SysMenu> sysMenuList = null;
        //如果是管理员查询所有菜单
        if( "1".equals(userId)) {
            QueryWrapper<SysMenu> wrapper = new QueryWrapper<>( );
            wrapper.eq( "status" , 1);
            wrapper.orderByAsc("sort_value" );
            sysMenuList = baseMapper.selectList(wrapper);
        }else {
            //普通用户查询对应的菜单
            sysMenuList = baseMapper.findMenuListUserId(userId);
        }
        //构建树形数据
        List<SysMenu> sysMenuTreeList = MenuHelper.buildTree(sysMenuList);
        //转换成前端需要的数据格式类型
        List<RouterVo> routerVoList = RouterHelper.buildRouters(sysMenuTreeList);
        return routerVoList;
    }
    //查询按钮权限
    @Override
    public List<String> getUserButtonList(String userId) {
        //菜单集合
        List<SysMenu> sysMenuList = null;
        //如果是管理员查询所有菜单
        if( "1".equals(userId)) {
            QueryWrapper<SysMenu> wrapper = new QueryWrapper<>( );
            wrapper.eq( "status" , 1);
            wrapper.orderByAsc("sort_value" );
            sysMenuList = baseMapper.selectList(wrapper);
        }else {
            //普通用户查询对应的菜单
            sysMenuList = baseMapper.findMenuListUserId(userId);
        }
        //创建返回的集合
        List<String> permissionList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            //类型(0:目录,1:菜单,2:按钮)
            if(sysMenu.getType() == 2){
                permissionList.add(sysMenu.getPerms());
            }
        }
        return permissionList;
    }
}
